<template>
  <div class="app-container">
    <!-- 查询 -->
    <tl-list-search ref="search" style="margin:6px 0" :query-param="queryParam" @search="doQuery" :isDisplayDefaultSearch="false" v-if="isHeaderDisplay">
      <template slot="search">
        <el-form-item label="关键信息" prop="name">
          <el-input v-model="queryParam.qry_name" placeholder="输入主题/标签" style="width:220px"></el-input>
        </el-form-item>
        <el-form-item label="会议类型">
          <el-cascader v-model="meetType" :options="meetTypeList" filterable @change="handleMeetTypeChange" clearable style="width:220px"></el-cascader>
        </el-form-item>
        <el-form-item label="会议时间"> <el-date-picker ref="datePicker" v-model="meettimerange" type="daterange" value-format="yyyy-MM-dd" align="right" unlink-panels range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions" @change="meetTimeChange"> </el-date-picker> </el-form-item>
      </template>
    </tl-list-search>
    <!-- 工具栏 -->
    <tl-list-toolbar class="tl-list-toolbar" :refMainTable="$refs.mainTable" currentModuleTableCode="mpmProjectCommunication/main" :startColumn="3" ref="toolbar" :isDisplayDeleteBatch="false" @add="openAddDialog('editForm')" @exportExcel="doExportExcel" @deleteBatch="doLimitDeleteBatch" @refresh="doRefresh" @columnCustomize="doColumnCustomize" @displaySearch="doDisplaySearch" :isDisplayExport="false" :disableDeleteBatch="selectedRowKeys.length <= 0">
      <template slot="left-btn"> </template>
    </tl-list-toolbar>
    <div>
      <!-- 列表选择 -->
      <div class="table-alert-div" ref="selector">
        <el-alert type="success" :closable="false" class="tl-table-alert-info" effect="light">
          <template slot="title">
            已选择&nbsp;<a class="tl-table-alert-info-a1">{{ selectedRowKeys.length }}</a
            >项&nbsp;&nbsp;
            <a class="tl-table-alert-info-a2" @click="doClearSelected">全部清空</a>
          </template>
        </el-alert>
      </div>
      <!--表格渲染-->
      <el-table class="tl-table" ref="mainTable" :height="tableHeight" resizable-key="tableHeight" :resizable-refs="tableResizeRefs" v-loading="loading" :data="datas" @selection-change="doSelectChange" @sort-change="doSortChange" @row-dblclick="doRowDbClick" @row-click="doRowClick" row-key="id" border fit :row-class-name="doChangeRowClassName">
        <el-table-column type="index" label="序号" width="55px" align="center"></el-table-column>
        <el-table-column prop="name" label="会议主题" min-width="180px" sortable="custom" sort-by="name" align="left" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="dispmeettypeid" label="会议类型" width="200px" sortable="custom" sort-by="meettypeid" align="left" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="place" label="会议地点" width="200px" sortable="custom" sort-by="place" align="left" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="begintime" label="会议开始时间" width="170px" sortable="custom" sort-by="begintime" align="center" header-align="center" :formatter="dateTimeFormat" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="endtime" label="会议结束时间" width="170px" sortable="custom" sort-by="endtime" align="center" header-align="center" :formatter="dateTimeFormat" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="keywords" label="会议标签" width="180px" sortable="custom" sort-by="keywords" align="left" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="dispstate" label="会议状态" width="100px" sortable="custom" sort-by="state" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="launchusername" label="会议发起人" width="100px" sortable="custom" sort-by="launchusercode" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="操作" width="95px" v-slot="scope" fixed="right">
          <el-dropdown trigger="hover">
            <el-button type="text" icon="el-icon-s-operation" style="margin-left:10px">更多<i class="el-icon-arrow-down el-icon--right"></i></el-button>
            <el-dropdown-menu>
              <el-dropdown-item icon="el-icon-edit" :disabled="disabledEdit(scope.row)" @click.native="openEditDialog('editForm', scope.row.id)">编辑</el-dropdown-item>
              <el-dropdown-item icon="el-icon-delete" :disabled="disabledEdit(scope.row)" @click.native="doLimitDelete(scope.row)">删除</el-dropdown-item>
              <el-dropdown-item icon="el-icon-view" @click.native="openViewDialog('editForm', scope.row.id)">查看</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-table-column>
      </el-table>
      <!-- 列表分页 -->
      <el-row class="pagebar" ref="pager">
        <el-pagination @size-change="doChangeSize" @current-change="doChangeIndex" :current-page="pageInfo.currentPage" :page-sizes="pageInfo.pageSizes" :total="pageInfo.totalCount" layout="total, sizes, prev, pager, next, jumper" background class="tl-table-pagination"> </el-pagination>
      </el-row>
      <tl-edit ref="editForm" :projectinfo="projectinfo" :queryParam="queryParam" @refresh="doRefresh"></tl-edit>
    </div>
  </div>
</template>

<script>
/** mixins */
import { mixin_list } from '@tapui/mixins/mixin_list'
/** 子组件 */
import tlEdit from './edit'
/** 工具类 */
import { formatDate } from '@tapui/utils/date'
import { getAction, putAction, postAction, deleteAction } from '@tapui/utils/http'
export default {
  name: 'sdProjectCommunication',
  mixins: [mixin_list],
  components: { tlEdit },
  data() {
    return {
      description: '项目沟通-列表查询',
      urlPrefix: process.env.MEET_API + '/meet/meetManage/',
      url: {
        queryCodeList: process.env.MEET_API + '/meet/meetManage/dataCodeList',
        queryMeetTypeList: process.env.MEET_API + '/meet/typeManage/queryTreeList',
        ifReviewed: '/sd/projectReview/ifReviewed'
      },
      // 排序参数
      sorterInfo: {
        sidx: 'begintime asc,id',
        sord: 'desc'
      },
      meetType: [],
      meetTypeList: [],
      meettimerange: [],
      isQueryOnCreated: false,
      reduceHeight: 264,
      pickerOptions: {
        shortcuts: [
          {
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              end.setTime(end.getTime() + 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              end.setTime(end.getTime() + 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              end.setTime(end.getTime() + 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            }
          }
        ]
      },
      queryParam: {
        qry_sourcetype: 'gpmProject',
        qry_sourceapp: 'sd',
        qry_sourceid: this.projectinfo.id
      }
    }
  },
  props: {
    // 项目信息
    projectinfo: {
      type: Object,
      default: () => {}
    }
  },
  computed: {
    disabledEdit() {
      return (row) => this.user.code !== row.launchusercode && this.user.code !== this.projectinfo.projectmanagercode && this.user.adminlevel < 9
    }
  },
  methods: {
    /** 打开新增对话框实现，添加复盘校验 */
    openAddDialog(editForm, rowData) {
      getAction(this.url.ifReviewed, { projectid: this.projectinfo.id }).then((res) => {
        if (res.success) {
          if (res.result) {
            this.$tap.alert('该项目已提交复盘审批，无法新增单据!')
          } else {
            if (!this.beforeOpenAddDialog(editForm, rowData)) {
              return
            }
            this.$refs[editForm].isAdd = true
            this.$refs[editForm].visible = true
            this.$refs[editForm].disableEditSubmit = false
            this.$nextTick(() => {
              this.afterOpenAddDialog(editForm, rowData)
            })
          }
        } else {
          this.$tap.alert(res.message, 'warning')
        }
      })
      .catch((err) => {
          this.$tap.alert(err, 'warninig')
      })
    },
    /** 限制删除*/
    doLimitDelete(data) {
      if (data.state === 1) {
        this.$tap.alert('发布的会议不允许删除！', 'warning')
        return
      }
      this.doDelete(data.id)
    },
    /** 批量删除 */
    doLimitDeleteBatch() {
      let isExistPublish = false
      if (this.selectedRowKeys.length <= 0) {
        this.$tap.alert('请选择记录！', 'warning')
        return
      } else {
        for (var index = 0; index < this.selectionRows.length; index++) {
          if (this.selectionRows[index].state === 1 || this.selectionRows[index].state === 2) {
            isExistPublish = true
          }
        }
      }
      if (isExistPublish === true) {
        this.$tap.alert('已经发布的会议不允许删除！请重新选择', 'warning')
        return
      } else {
        this.doDeleteBatch()
      }
    },
    handleMeetTypeChange(value) {
      this.queryParam.qry_meettypeid = value[value.length - 1]
    },
    meetTimeChange(value) {
      if (value == null) {
        this.queryParam.qry_begindatetime = ''
        this.queryParam.qry_enddatetime = ''
      } else {
        this.queryParam.qry_begindatetime = value[0]
        this.queryParam.qry_enddatetime = value[1]
      }
    },
    /** 表格双击事件 */
    doRowDbClick(row, column, event) {
      if (this.disabledEdit(row)) {
        // 判断是否有权限
        this.openViewDialog('editForm', row.id)
      } else {
        this.openEditDialog('editForm', row.id)
      }
    },
    initSearchInfo() {
      /** 查询树 */
      getAction(this.url.queryMeetTypeList, { isAddRoot: false }).then((res) => {
        if (res.success) {
          let reg = new RegExp('id', 'g') // g代表全部
          this.meetTypeList = JSON.parse(JSON.stringify(res.result.dataList).replace(reg, 'value'))
        }
      })
      this.$nextTick(() => {
        const end = new Date()
        const start = new Date()
        // 开始时间提前三天
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 1)
        // 结束时间三个月
        end.setTime(end.getTime() + 3600 * 1000 * 24 * 90)
        if (this.$refs['datePicker']) {
          this.$refs['datePicker'].$emit('pick', [start, end])
        }
        this.meettimerange = [formatDate(start, 'yyyy-MM-dd'), formatDate(end, 'yyyy-MM-dd')]
        this.meetTimeChange(this.meettimerange)
        this.doQuery()
      })
    }
  },
  created() {
    this.initSearchInfo()
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  margin: 0;
}
</style>
